package com.ruoyi.wxhouse.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.house.domain.YjzUser;
import com.ruoyi.wxhouse.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序用户控制器
 */
@RestController
@RequestMapping("/wx")
public class WxUserController {

    @Autowired
    private IWxUserService wxUserService;

    /**
     * 小程序登录注册
     * @param loginData
     * @return 登录信息
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, Object> loginData) {
        String code = (String) loginData.get("code");
        String nickName = (String) loginData.get("nickname");
        String avatarUrl = (String) loginData.get("avatar");

        // 正确地创建并填充 Map
        Map<String, Object> yjzwxuser = new HashMap<>();
        yjzwxuser.put("code", code);
        // 如果需要，也可以继续添加其他字段
        yjzwxuser.put("nickname", nickName);
        yjzwxuser.put("avatar", avatarUrl);


        return wxUserService.login(yjzwxuser);
    }

    /**
     * 获取用户信息
     */
    @PostMapping("/info")
    public AjaxResult info(@RequestParam("userId") Long userId) {
        YjzUser userInfo = wxUserService.getUserInfo(userId);
        return AjaxResult.success(userInfo);
    }



}